package com.china08.yxyapi.schedule.homework;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.china08.yxyapi.entity.mg.homework.HomeworkTaskClass;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskReport;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskClassRepository;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskReportQuesRepository;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskReportRepository;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskRepository;
import com.china08.yxyapi.service.manage.stu.mystudy.homework.dowork.impl.HomeworkServiceImpl;
import com.china08.yxyapi.util.DateAndLocalDateConvertUtils;
import com.china08.yxyapi.util.YxUserPointsUtilV3;

@Component
@EnableScheduling
public class HomeworkTaskReportUpdateTask {

    private static final Logger LOGGER = LoggerFactory.getLogger(HomeworkTaskReportUpdateTask.class);

    // 在多少分钟之前统计
    private static final Integer minutesIntervalCou = 10;

    @Autowired
    public HomeworkTaskReportRepository homeworkTaskReportRepository;

    @Autowired
    public HomeworkTaskReportQuesRepository homeworkTaskReportQuesRepository;

    @Autowired
    public HomeworkTaskClassRepository homeworkTaskClassRepository;

    @Autowired
    public HomeworkServiceImpl homeworkService;

    @Autowired
    public YxUserPointsUtilV3 yxUserPointsUtilV3;

    @Autowired
    public HomeworkTaskRepository homeworkTaskRepository;

    // 统计排名
    @Scheduled(fixedRate = 1000 * 60 * 5)
    public void myRanking() {
        // LOGGER.info("统计开始前三名排名...");
        List<Integer> statusOfFinishedLst = Arrays.asList(new Integer[] { 1, 2 });
        Date predictDate = DateAndLocalDateConvertUtils.asDate(LocalDateTime.now().minusMinutes(minutesIntervalCou));
        // 1. 查询符合条件的任务下的所有班级
        List<HomeworkTaskClass> homeworkTaskClassLst = homeworkTaskClassRepository
                .findByPredictDateGreaterThan(predictDate);
        List<HomeworkTaskReport> homeworkTaskReportList = null;
        for (HomeworkTaskClass homeworkTaskClass : homeworkTaskClassLst) {
            // 2.查询所有学生的答题记录（多少个学生就有多少条记录）并且按照每个学生的正确率倒序排序
            homeworkTaskReportList = homeworkTaskReportRepository
                    .findByTaskIdAndClassIdAndStatusOfFinishedInOrderByRightRateDesc(homeworkTaskClass.getTaskId(),
                            homeworkTaskClass.getClassId(), statusOfFinishedLst);
            for (int i = 0; i < homeworkTaskReportList.size(); i++) {
                // 3.将班级的前三名给排出来保存到实体类中
                homeworkTaskReportList.get(i).setMyRanking(i + 1);
            }
            homeworkTaskReportRepository.save(homeworkTaskReportList);
        }
    }

    // 修改 截止日期后 未完成 作业同学状态为 预期未交
    @Scheduled(fixedRate = 1000 * 60 * 2)
    public void statusOfFinishedUpd() {
        // LOGGER.info("统计修改 截止日期后 未完成 作业同学状态为 预期未交...");
        // 修改未做的
        Date currentDate = new Date();
        List<HomeworkTaskReport> homeworkTaskReportList = homeworkTaskReportRepository
                .findByStatusOfFinishedAndPredictDateLessThanOrderByRightRateDesc(0, currentDate);
        for (HomeworkTaskReport homeworkTaskReport : homeworkTaskReportList) {
            homeworkTaskReport.setStatusOfFinished(-1);
            homeworkService.timeOver(homeworkTaskReport.getStudentId(), homeworkTaskReport.getTaskId());
        }
        homeworkTaskReportRepository.save(homeworkTaskReportList);

        // 修改答题中
        List<HomeworkTaskReport> homeworkTaskReportList2 = homeworkTaskReportRepository
                .findByStatusOfFinishedAndPredictDateLessThanOrderByRightRateDesc(3, currentDate);
        for (HomeworkTaskReport homeworkTaskReport : homeworkTaskReportList2) {
            homeworkTaskReport.setStatusOfFinished(-1);
            homeworkService.timeOver(homeworkTaskReport.getStudentId(), homeworkTaskReport.getTaskId());
        }
        homeworkTaskReportRepository.save(homeworkTaskReportList2);
    }
}
